﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Win32;
using System.IO;
using System.Security.AccessControl;
using CustomsSystem.BLL;

namespace WEB.Form
{
    public class baseFile : System.Web.UI.Page
    {
        public info_formMaster fmBll = new info_formMaster();
        public info_form fBll = new info_form();
        #region 文件操作
        //// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="filename">文件物理地址</param>
        public void FormDownloadFile(string filename)
        {
            int intStart = filename.LastIndexOf(@"\") + 1;
            string saveFileName = filename.Substring(intStart, filename.Length - intStart);
            System.IO.FileInfo fi = new System.IO.FileInfo(filename);
            string fileextname = fi.Extension;
            string DEFAULT_CONTENT_TYPE = "application/unknown";
            RegistryKey regkey, fileextkey;
            string filecontenttype;
            try
            {
                regkey = Registry.ClassesRoot;
                fileextkey = regkey.OpenSubKey(fileextname);
                filecontenttype = fileextkey.GetValue("Content Type", DEFAULT_CONTENT_TYPE).ToString();
            }
            catch
            {
                filecontenttype = DEFAULT_CONTENT_TYPE;
            }
            Response.Clear();
            Response.Charset = "gb2312";
            Response.Buffer = true;
            this.EnableViewState = false;
            Response.ContentEncoding = System.Text.Encoding.Default;
            string encodefileName = System.Web.HttpUtility.UrlEncode(saveFileName, System.Text.Encoding.UTF8);
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + encodefileName);
            Response.ContentType = filecontenttype;

            Response.WriteFile(filename);
            Response.Flush();
            Response.Close();

            Response.End();
        }
        /// <summary>
        /// 复制文件
        /// </summary>
        /// <param name="srcPath"></param>
        /// <param name="aimPath"></param>
        public void CopyDir(string srcPath, string newPath)
        {
            try
            {

                // 判断目标目录是否存在如果不存在则新建
                if (System.IO.File.Exists(srcPath))
                {
                    System.IO.File.Copy(srcPath, newPath, true);//复制文件
                    SetAccount(newPath);//赋予权限
                }
            }
            catch (Exception e)
            {
                return;
            }
        }
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="path"></param>
        public void DelDir(string path)
        {
            try
            {
                // 判断目标目录是否存在如果不存就不用删除
                if (System.IO.File.Exists(path))
                {
                    System.IO.File.Delete(path);//删除文件
                }
            }
            catch (Exception e)
            {
                return;
            }
        }
        /// <summary>
        /// 权限赋予
        /// </summary>
        /// <param name="filePath">文件地址</param>
        public void SetAccount(string filePath)
        {
            FileInfo fileInfo = new FileInfo(filePath);//获取文件
            FileSecurity fileSecurity = fileInfo.GetAccessControl();
            fileSecurity.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow));     //以完全控制为例
            fileInfo.SetAccessControl(fileSecurity);
        }
        #endregion
    }
}